package com.venutech.gongan.api;

import com.baomidou.mybatisplus.plugins.Page;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import com.venutech.gongan.entity.DevicePermission;
import com.venutech.gongan.entity.DevicePermissionHistory;
import com.venutech.gongan.entity.Qdsxj;
import com.venutech.gongan.enums.DevicePermissionStatus;
import com.venutech.gongan.model.ResultFormat;
import com.venutech.gongan.service.DevicePermissionService;
import com.venutech.gongan.service.QdsxjService;
import com.venutech.gongan.util.ResultUtil;
import com.venutech.gongan.vo.DevicePermissionHistoryVO;
import com.venutech.gongan.vo.DevicePermissionUserVO;
import com.venutech.gongan.vo.DevicePermissionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 设备权限
 * 1. 警务通申请接口
 * 2. 查看自己的申请列表和状态
 *
 * 3. 查看设备拥有的权限用户列表
 * 4. 查看所有的权限列表
 * 5. 查看申请记录
 * 6. 通过申请
 * 7. 拒绝申请
 */
@Api(tags = "设备权限")
@RestController
@RequestMapping("devicePermission")
public class DevicePermissionController extends AbstractControllerBase {
    @Autowired
    private DevicePermissionService devicePermissionService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private QdsxjService qdsxjService;

    @ApiOperation("获取指定设备的用户列表")
    @PostMapping("userList")
    public DevicePermissionUserVO findByDeviceId(
            @ApiParam("设备id")
            @RequestParam("deviceId") int deviceId) {
        Qdsxj qdsxj = qdsxjService.getById(deviceId);
        List<DevicePermission> permissions = devicePermissionService.findByDeviceId(deviceId)
                .stream()
                .filter(devicePermission -> DevicePermissionStatus.ENABLE.getStatus().equals(devicePermission.getStatus()))
                .collect(Collectors.toList());
        List<Long> sysUserIds = permissions.stream().map(DevicePermission::getUserId).map(Integer::longValue).collect(Collectors.toList());
        List<SysUser> sysUsers = sysUserService.findByUserIds(sysUserIds);

        return new DevicePermissionUserVO(qdsxj, sysUsers);
    }

    @ApiOperation("禁用用户权限")
    @PostMapping("disable")
    public ResultFormat deleteDevicePermission(@RequestParam("deviceId") int deviceId, @RequestParam("userId") int userId) {
        devicePermissionService.removeDevicePermission(deviceId, userId, currentUserId().intValue());

        return ResultUtil.success();
    }

    @ApiOperation("允许用户申请权限")
    @PostMapping("enable-apply")
    public ResultFormat enableDevicePermissionApply(@RequestParam("deviceId") int deviceId, @RequestParam("userId") int userId) {
        devicePermissionService.enableDevicePermissionApply(deviceId, userId, currentUserId().intValue());

        return ResultUtil.success();
    }

    @ApiOperation("新增用户权限")
    @PostMapping("create")
    public ResultFormat createDevicePermission(@RequestParam("deviceId") int deviceId, @RequestParam("userId") int userId) {
        devicePermissionService.addDevicePermission(deviceId, userId, currentUser().getUserId().intValue());

        return ResultUtil.success();
    }

    @ApiOperation("查看所有申请列表")
    @PostMapping("list")
    public Page<DevicePermissionVO> devicePermissionList(@RequestParam("page") int page, @RequestParam("size") int size) {
        return devicePermissionService.findAll(page, size);
    }

//
//    @ApiOperation("保存、更新用户权限")
//    @PostMapping("update")
//    public ResultFormat updateDevicePermission(@RequestParam("deviceId") int deviceId, @RequestParam("userIds") String userIds) {
//        devicePermissionService.updateDevicePermission(deviceId, Arrays.stream(userIds.split(","))
//                        .filter(StringUtils::isNotEmpty)
//                        .map(Integer::parseInt)
//                .collect(Collectors.toList()), currentUser().getUserId().intValue());
//
//        return ResultUtil.success();
//    }


    @ApiOperation("查看申请历史")
    @PostMapping("history")
    public ResultFormat devicePermissionHistory(@RequestParam("deviceId") int deviceId, @RequestParam("userId") int userId) {
        List<DevicePermissionHistory> histories = devicePermissionService.findHistory(deviceId, userId);

        List<DevicePermissionHistoryVO> vos = histories.stream()
                .map(devicePermissionHistory -> new DevicePermissionHistoryVO(devicePermissionHistory, sysUserService.selectUserById(devicePermissionHistory.getOperatorId().longValue())))
                .collect(Collectors.toList());

        return ResultUtil.success(vos);
    }

    // 警务通

    @ApiOperation("警务通：申请设备权限")
    @PostMapping("jwt/apply")
    public ResultFormat applyDevicePermission(@RequestParam("deviceId") int deviceId) {
        devicePermissionService.requestDevicePermission(currentUserId().intValue(), deviceId);

        return ResultUtil.success();
    }

    @ApiOperation("警务通:查看申请历史")
    @PostMapping("jwt/history")
    public ResultFormat devicePermissionJwtHistory(@RequestParam("deviceId") int deviceId) {
        List<DevicePermissionHistory> histories = devicePermissionService.findHistory(deviceId, currentUserId().intValue());

        List<DevicePermissionHistoryVO> vos = histories.stream()
                .map(devicePermissionHistory -> new DevicePermissionHistoryVO(devicePermissionHistory, sysUserService.selectUserById(devicePermissionHistory.getOperatorId().longValue())))
                .collect(Collectors.toList());

        return ResultUtil.success(vos);
    }
}
